"""
Case Type   : 服务端工具
Case Name   : root用户检查SSD性能
Create At   : 2025/2/27
Owner       : sungang14
Description :
    1.切换到root用户下
    2.source环境变量
    3.gs_checkperf -U 运行openGauss的用户名称 -i SSD
Expect      :
    1.切换到root用户下
    2.source环境变量
    3.检查成功
History     :
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = Node('PrimaryDbUser')
        self.std1_node = Node('Standby1DbUser')
        self.std2_node = Node('Standby2DbUser')
        self.root_node = Node('PrimaryRoot')
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH
        self.log.info('创建root互信')
        self.common.create_root_trust()

    def test_tools_checkperf(self):
        text = '----step1:root用户下执行gs_checkperf -U 运行openGauss的用户名称 -i SSD;expect:检查成功----'
        self.log.info(text)
        sql_cmd = f"source {self.env_path};gs_checkperf -U {self.user_node.ssh_user} -i SSD"
        self.log.info(sql_cmd)
        sql_res = self.common.get_sh_result(self.root_node, sql_cmd)
        self.log.info(sql_res)
        if f"[GAUSS-53008] : The current node do not install SSD. Can not check SSD performance" in sql_res:
            self.log.info('当前节点未安装SSD，无法检查SSD性能')
            return
        else:
            self.assertIn('Cluster statistics information', sql_res, '执行失败' + text)

    def tearDown(self):
        self.log.info(f"无需清理环境")
        self.log.info(f'----{os.path.basename(__file__)} end----')
